package com.koron.css2.newProjectCenter.impl.inforManag;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.newProjectCenter.bean.query.PubUpNewsQueryBean;
import com.koron.css2.newProjectCenter.mapper.PubUpNewsMapper;
import com.koron.css2.newProjectCenter.vo.PubUpNewsListVO;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;

/**
 * 工程款调整列表查询
 * @author xusheng
 *
 */
public class PubUpNewsList implements ServerInterface {
	
	@Override
	@SuppressWarnings("rawtypes")
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		try {
			//将入参转换为QueryBean
			PubUpNewsQueryBean queryBean = JsonUtils.objectToPojo(req.getData(), PubUpNewsQueryBean.class);
			queryBean.setRecipient(userInfo.getUserInfo().getAcount());

			//获取Mapper实例
			PubUpNewsMapper mapper = factory.getMapper(PubUpNewsMapper.class);
			if(queryBean.getToDo()!=null && queryBean.getToDo()==2){
				return  update(mapper,queryBean);
			}
			if(queryBean.getToDo()!=null &&  queryBean.getToDo()==3){
				return  clearMessage(mapper,queryBean);
			}

			//设置分页参数
			PageHelper.startPage(queryBean.getPage(), queryBean.getPageCount());
			//执行SQL
			List<PubUpNewsListVO> list = mapper.selecList(queryBean);
			/*//获取来源渠道数据字典
			Map<String, String> PIS = Tools.mapDicByCode(factory, "PIS");
			//获取工程类型数据字典
			Map<String, String> PPT = Tools.mapDicByCode(factory, "PPT");
			 */
			//输出分页结果
			MessageBean<PageInfo> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", PageInfo.class);
			info.setData(new PageInfo<>(list));
			return info;
		}catch(Exception e){
			factory.close(false);
			logger.error("查询失败"+e.getMessage(), e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "查询失败", null);
		}
	}


	private   MessageBean<?>  update(PubUpNewsMapper thisMapper,PubUpNewsQueryBean queryBean){
		if(StringUtils.isEmpty(queryBean.getId())){
			return MessageBean.create(Constant.NOT_NULL, "消息主键不能为空", null);
		}
		MessageBean<Integer> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "修改成功", Integer.class);
		Integer num= thisMapper.updateById(queryBean);
		info.setData(num);
		return   info ;
	}

	private   MessageBean<?>  clearMessage(PubUpNewsMapper thisMapper,PubUpNewsQueryBean queryBean){
		if(StringUtils.isEmpty(queryBean.getId())){
			return MessageBean.create(Constant.NOT_NULL, "消息主键不能为空", null);
		}
		MessageBean<Integer> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "修改成功", Integer.class);
		Integer num= thisMapper.clearMessage(queryBean.getIds(),queryBean.getRecipient());
		info.setData(num);
		return   info ;
	}
}
